package com.team2.outpatient_sys.service.impl;

import com.team2.outpatient_sys.dao.*;
import com.team2.outpatient_sys.pojo.*;
import com.team2.outpatient_sys.service.InventoryService;
import com.team2.outpatient_sys.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    private PharmacyStorageDao pharmacyStorageDao;

    @Autowired
    private MedicineDao medicineDao;

    @Autowired
    private UnqualifiedMedicineDao unqualifiedMedicineDao;

    @Autowired
    private SupplementDao supplementDao;

    @Autowired
    private storageOutDao storageOutDao;

    //查询库存不足的信息
    @Override
    public List<PharmacyStorage> reminderUnderstock() {
        return pharmacyStorageDao.findByCont();
    }

    @Override
    public List<PharmacyStorage> deadlineRemind() {
        //获取所有的药品信息
        List<PharmacyStorage> all = pharmacyStorageDao.findAll();

        ArrayList<PharmacyStorage> pharmacyStorages = new ArrayList<>();
        //遍历出所有的时间,计算到现在为止还有多少天的保质期
        for (PharmacyStorage pharmacyStorage : all) {
            String dateManufacture = pharmacyStorage.getDateManufacture();
            long time = DateUtils.stringToDate(dateManufacture).getTime();
            long l = System.currentTimeMillis();

            //保质期低于90天的药品需要返回

            if (pharmacyStorage.getDeadline()-(l-time)/1000/60/60/24 <= 90){
                pharmacyStorages.add(pharmacyStorage);
                System.out.println(pharmacyStorage.getDeadline()-(l-time)/1000/60/60/24);
            }
        }
        return pharmacyStorages;
    }

    @Override
    public Integer addUnqualifiedMedicine(UnqualifiedMedicine unqualifiedMedicine) {
        //先减少库存数量，再把有问题的药品和数量存到问题表
        pharmacyStorageDao.update(unqualifiedMedicine.getCont(),unqualifiedMedicine.getMedicineId());
        return unqualifiedMedicineDao.add(unqualifiedMedicine);
    }

    //查询所有的库房信息
    @Override
    public List<PharmacyStorage> findAllPharmacyStorage() {
        return pharmacyStorageDao.findAll();
    }

    //查询问题表中所有没有退回的数据
    @Override
    public List<ShowUnqualifiedMedicine> storageOutNO() {
        return unqualifiedMedicineDao.findAllNO();
    }

    @Override
    public List<ShowUnqualifiedMedicine> storageOutYES() {
        return unqualifiedMedicineDao.findAllYES();
    }

    @Override
    public Integer UpdateStorageOut(UnqualifiedMedicine unqualifiedMedicine) {
        //把出库的药品信息插入到数据库中
        StorageOut storageOut = new StorageOut();
        storageOut.setMedicineId(unqualifiedMedicine.getMedicineId());
        storageOut.setOutNum(unqualifiedMedicine.getCont());
        storageOut.setOutTime(DateUtils.getDate());

        Integer add = storageOutDao.add(storageOut);
        return unqualifiedMedicineDao.UpdateStorageOut(unqualifiedMedicine.getId());
    }

    @Override
    public List<Supplement> findAllSupplement() {
        return supplementDao.findAll();
    }

    @Override
    public Integer updateSupplement(Supplement supplement) {
        return supplementDao.updateSupplement(supplement.getId());
    }

    @Override
    public Integer addSupplement(Supplement supplement) {
        return supplementDao.add(supplement);
    }

    @Override
    public Integer update(Supplement supplement) {
        return supplementDao.update(supplement);
    }

    @Override
    public Integer updateError(UnqualifiedMedicine unqualifiedMedicine) {
        return unqualifiedMedicineDao.update(unqualifiedMedicine);
    }
}
